home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 January: Mac OS SDK / Dev.CD Jan 00 SDK1.toast / Development Kits / Mac OS / Multiprocessing 2.0 SDK / Sample Code / HappyTrails ƒ / µApp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-03  |  7.5 KB  |  437 lines  |  [TEXT/CWIE]

  1. /**\
  2. |**|    µApp.c
  3. \**/
  4.  
  5. /**\
  6. |**|    compiler directives
  7. \**/
  8.  
  9. #define SystemSevenOrLater        1
  10.  
  11. /**\
  12. |**|    Includes
  13. \**/
  14.  
  15. #include <Devices.h>
  16. #include <Fonts.h>
  17. #include <CodeFragments.h>
  18. #include <Dialogs.h>
  19. #include <DiskInit.h>
  20. #include <LowMem.h>
  21. #include <fp.h>
  22. #include <PLStringFuncs.h>
  23. #include <Sound.h>
  24. #include <Traps.h>
  25. #include <Threads.h>
  26.  
  27. #include <stdio.h>
  28. #include <string.h>
  29.  
  30. #include "HappyTrails.h"
  31.  
  32. /**\
  33. |**|    typedefs, structs, enums, etc.
  34. \**/
  35.  
  36. enum
  37. {
  38.     mAppleMenu = 128,
  39.         iAboutBox = 1,
  40.     mFileMenu = 129,
  41.         iQuit = 1,
  42.     mTasksMenu = 130,
  43.     mWeightMenu = 131
  44. };
  45.  
  46. /**\
  47. |**|    private (local) function prototypes
  48. \**/
  49.  
  50. static OSErr InitMac(void);
  51. static void Command(long ms);
  52. static void DoEvent(const EventRecord *pEventPtr);
  53. static void DoNullEvent(const EventRecord *pEventPtr);
  54. static void DoMouseEvent(const EventRecord *pEventPtr);
  55. static void DoKeyEvent(char key,SInt16 modifiers);
  56. static void DoUpdateEvent(WindowPtr updateWindowP);
  57. static void DoActivateEvent(WindowPtr updateWindowP);
  58. static void DoDiskEvent(long message);
  59. static void DoOSEvent(long message);
  60. static void doContentClick(WindowPtr pWindowPtr,const EventRecord *pEventPtr);
  61.  
  62. static Boolean SetUpMenuBar(void);
  63. static void AdjustMenuItems(void);
  64.  
  65. /**\
  66. |**|    globals & constants
  67. \**/
  68.  
  69. WindowPtr    gWindowPtr = nil;
  70. Rect        gWindowRect;
  71. Boolean        gInBackGround = false;
  72. Rect         gMenuRect = {0,0,0,0};
  73.  
  74. static Boolean gQuitFlag = false;
  75.  
  76. /**\
  77. |**|    main
  78. \**/
  79.  
  80. void main(void)
  81. {
  82.     if (!InitMac() && SetUpMenuBar() && !HT_Init(9))
  83.     {
  84.         do
  85.         {
  86.             EventRecord event;
  87.             InitCursor();
  88.             WaitNextEvent(everyEvent,&event,-1,nil);
  89.             DoEvent(&event);
  90.         }
  91.         while (!gQuitFlag);
  92.  
  93.         HT_Term();
  94.     }
  95. }
  96.  
  97. /**\
  98. |**|    Initialize toolboxes
  99. \**/
  100.  
  101. static OSErr InitMac(void)
  102. {
  103.     OSErr        error;
  104.     SysEnvRec    theWorld;
  105.         
  106.     //
  107.     //    Test the computer to be sure we can do color.  
  108.     //    If not we would crash, which would be bad.  
  109.     //    If we can’t run, just beep and exit.
  110.     //
  111.  
  112.     error = SysEnvirons(1, &theWorld);
  113.     if (theWorld.hasColorQD == false)
  114.     {
  115.         SysBeep(50);
  116.         ExitToShell();                    // If no color QD, we must leave.
  117.     }
  118.     
  119.     MaxApplZone();
  120.     InitGraf(&(qd.thePort));
  121.     InitFonts();
  122.     InitWindows();
  123.     InitMenus();
  124.     TEInit();
  125.     InitDialogs(nil);
  126.  
  127.     InitContextualMenus();
  128.  
  129.     //
  130.     //    Make a new window for drawing in, and it must be a color window.  
  131.     //    The window is full screen size, made smaller to make it more visible.
  132.     //
  133.  
  134.     gWindowRect = qd.screenBits.bounds;
  135. //    InsetRect(&gWindowRect, 20, 20);
  136.     gWindowRect.top += LMGetMBarHeight() + 1;
  137.  
  138.     gWindowPtr = NewCWindow(nil, &gWindowRect, "\pHappy Trails!", true, documentProc, 
  139.                         (WindowPtr) -1, false, 0);
  140.  
  141.     OffsetRect(&gWindowRect, -gWindowRect.left, -gWindowRect.top);
  142.  
  143.     SetPort(gWindowPtr);                    // set window to current graf port
  144.     TextSize(kTextSize);                    // smaller font for drawing.
  145.  
  146.     return noErr;
  147. }
  148.  
  149. /**\
  150. |**|    Menu hooks
  151. \**/
  152.  
  153. static pascal short MyMBarHook(Rect *menuRect)
  154. {
  155. //    RgnHandle tempRgn = NewRgn();
  156.  
  157.     gMenuRect = *menuRect;
  158.  
  159. //    RectRgn(tempRgn,menuRect);
  160. //    CalcVisBehind(LMGetWindowList(),tempRgn);
  161. //    InvertRgn(tempRgn);
  162.  
  163.     //YieldToAnyThread();
  164. //    DisposeRgn(tempRgn);
  165.     return 0;
  166. }
  167.  
  168. static pascal void MyMenuHook(void)
  169. {    
  170. //    RgnHandle tempRgn = NewRgn();
  171. //    RectRgn(tempRgn,&gMenuRect);
  172. //    CalcVisBehind(LMGetWindowList(),tempRgn);
  173. //    InvertRgn(tempRgn);
  174.  
  175.     //YieldToAnyThread();
  176. //    DisposeRgn(tempRgn);
  177. }
  178.  
  179. /**\
  180. |**|    Setup menu bar
  181. \**/
  182.  
  183. static Boolean SetUpMenuBar(void)
  184. {
  185.     Handle mBar = GetNewMBar(128);    // handle to menu bar resource
  186.  
  187.     if (!ResError() && mBar)
  188.     {
  189.         SetMenuBar(mBar);
  190.         AppendResMenu(GetMenuHandle(mAppleMenu),'DRVR');
  191.         DrawMenuBar();
  192.         ReleaseResource(mBar);
  193.         AdjustMenuItems();
  194.  
  195.         LMSetMBarHook(NewMBarHookProc(MyMBarHook));
  196.         LMSetMenuHook(NewMenuHookProc(MyMenuHook));
  197.  
  198.         return true;
  199.     }
  200.     return false;
  201. }
  202.  
  203. /**\
  204. |**|    Adjust menu items
  205. \**/
  206.  
  207. static void AdjustMenuItems(void)
  208. {
  209.     MenuHandle tMenuHdl = GetMenuHandle(mTasksMenu);
  210.     if (tMenuHdl)
  211.     {
  212.         UInt16 index,count = CountMItems(tMenuHdl);
  213.         for (index = 1;index <= count;index++)
  214.             CheckItem(tMenuHdl, index, index == gNumTasks);
  215.     }
  216. }
  217.  
  218. /**\
  219. |**|    Do menu command
  220. \**/
  221.  
  222. static void Command(long ms)
  223. {
  224.     short    menuID = ms >> 16,
  225.             menuItem = ms & 0xFFFF;
  226.  
  227.     switch (menuID)
  228.     {
  229.         case    mAppleMenu:
  230.             switch (menuItem)
  231.             {
  232.                 case iAboutBox:        // Bring up alert for About.
  233.                     SysBeep(15);
  234.                     break;
  235.                 default:            // All non-About items in this menu are DAs.
  236.                     {
  237.                         Str255    daName;
  238.                         GetMenuItemText(GetMenuHandle(menuID),menuItem,daName);
  239.                         OpenDeskAcc(daName);
  240.                     }
  241.                     break;
  242.             }
  243.             break;
  244.         case    mFileMenu:
  245.             switch (menuItem)
  246.             {
  247.                 case iQuit:
  248.                     gQuitFlag = true;
  249.                     break;
  250.                 default:
  251.                     SysBeep(15);
  252.                     break;
  253.             }
  254.             break;
  255.         case    mTasksMenu:
  256.             HT_Term();
  257.             HT_Init(menuItem);
  258.             break;
  259.         case    mWeightMenu:        // Weight Menu
  260.             break;
  261.     }
  262. }
  263.  
  264. /**\
  265. |**|    Do event
  266. \**/
  267.  
  268. static void DoEvent(const EventRecord *pEventPtr)
  269. {
  270.     switch (pEventPtr->what)
  271.     {
  272.         case nullEvent:
  273.             DoNullEvent(pEventPtr);
  274.             break;
  275.         case mouseDown:
  276.             DoMouseEvent(pEventPtr);
  277.             break;
  278.         case keyDown:
  279.         case autoKey:
  280. dokey:
  281.             DoKeyEvent((char)(pEventPtr->message & charCodeMask),pEventPtr->modifiers);
  282.             break;
  283.         case updateEvt:
  284.             DoUpdateEvent((WindowPtr)pEventPtr->message);
  285.             break;
  286.         case diskEvt:
  287.             DoDiskEvent(pEventPtr->message);
  288.             break;
  289.         case osEvt:
  290.             DoOSEvent(pEventPtr->message);
  291.             break;
  292.         case kHighLevelEvent:
  293.             AEProcessAppleEvent(pEventPtr);
  294.             break;
  295.         default:
  296.             break;
  297.     }
  298. }
  299.  
  300. /**\
  301. |**|    Do null event
  302. \**/
  303.  
  304. static void DoNullEvent(const EventRecord *pEventPtr)
  305. {
  306.     (pEventPtr);    // #pragma unused (pEventPtr)
  307.     if (gWindowPtr)
  308.     {
  309.         Rect    tRect = {40,20,52,128};
  310.         const RGBColor blackRGBColor = {0,0,0}, yellowRGBColor = {255,255,0};
  311.         GrafPtr savePort;
  312.  
  313.         GetPort(&savePort);
  314.         SetPort(gWindowPtr);
  315.  
  316.         HT_DoNull();
  317.  
  318.         SetPort(savePort);
  319.     }
  320. }
  321.  
  322. /**\
  323. |**|    Do mousedown event
  324. \**/
  325.  
  326. static void DoMouseEvent(const EventRecord *pEventPtr)
  327. {
  328.     Boolean tInBackGround = gInBackGround;
  329.     WindowPtr            window;
  330.     short                part;
  331.  
  332.     gInBackGround = true;    // suspend drawing while menus are displayed
  333.  
  334.     part = FindWindow(pEventPtr->where,&window);
  335.  
  336.     if (part != inContent)
  337.         SetCursor(&qd.arrow);
  338.  
  339.     switch(part)
  340.     {
  341.         case inContent:
  342.             {
  343.                 if (window != FrontWindow())
  344.                     SelectWindow(window);
  345.                 else
  346.                     HT_DoClick(pEventPtr);
  347.             }
  348.             break;
  349.  
  350.         case inMenuBar:        // Process mouse menu command (if any).
  351.             {
  352.                 long ms;
  353.  
  354.                 AdjustMenuItems();
  355.                 ms = MenuSelect(pEventPtr->where);
  356.                 if (ms)
  357.                     Command(ms);
  358.                 HiliteMenu(0);        // Unhighlight what MenuSelect hilited.
  359.             }
  360.             break;
  361.  
  362.         case inSysWindow:    // Let the system handle the mouseDown.
  363.             SystemClick(pEventPtr,window);
  364.             break;
  365.  
  366.         default:
  367.             break;
  368.     }
  369.     gInBackGround = tInBackGround;
  370.  
  371.     SetRect(&gMenuRect, 0, 0, 0, 0);
  372. }
  373.  
  374. /**\
  375. |**|    Do key events
  376. \**/
  377.  
  378. static void DoKeyEvent(char key,SInt16 modifiers)
  379. {
  380.     if ((modifiers & cmdKey) != 0)
  381.     {
  382.         AdjustMenuItems();
  383.         Command(MenuKey(key));
  384.         HiliteMenu(0);        // Unhighlight what MenuSelect hilited.
  385.     }
  386. }
  387.  
  388. /**\
  389. |**|    Do update events
  390. \**/
  391.  
  392. static void DoUpdateEvent(WindowPtr updateWindowP)
  393. {
  394.     GrafPtr savePort;
  395.  
  396.     GetPort(&savePort);
  397.     SetPort(updateWindowP);
  398.     BeginUpdate(updateWindowP);
  399.  
  400.     UpdateControls(updateWindowP,updateWindowP->visRgn);
  401.  
  402.     DrawGrowIcon(updateWindowP);
  403.     EndUpdate(updateWindowP);
  404.  
  405.     SetPort(savePort);
  406. }
  407.  
  408. /**\
  409. |**|    Do disk events
  410. \**/
  411.  
  412. static void DoDiskEvent(long message)
  413. {
  414.     Point dialogLocation = {100,100};
  415.  
  416.     if ((message & 0xFFFF0000) != noErr)
  417.     {
  418.         DIBadMount(dialogLocation,message);
  419.     }
  420. }
  421.  
  422. /**\
  423. |**|    Do OS events
  424. \**/
  425.  
  426. static void DoOSEvent(long message)
  427. {
  428.     if ((message >> 24) == suspendResumeMessage)
  429.     {
  430.         if ((message & resumeFlag) != 0)
  431.             gInBackGround = false;
  432.         else
  433.             gInBackGround = true;
  434.     }
  435. }
  436.  
  437.